package com.oliet.controller;


import com.oliet.common.dto.role.CreateRoleDTO;
import com.oliet.common.dto.role.UpdateRoleDTO;
import com.oliet.common.result.CommonResponse;
import com.oliet.common.vo.page.PageVO;
import com.oliet.common.vo.role.RoleVO;
import com.oliet.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author JZP
 * @since 2023-11-26
 */
@RestController
@RequestMapping("/role")
public class RolesController {

    @Autowired
    private RolesService rolesService;

    @PostMapping("/create")
    public CommonResponse create(@Validated @RequestBody CreateRoleDTO param) {
        this.rolesService.create(param);
        return CommonResponse.success();
    }

    @PutMapping("/update")
    public CommonResponse update(@Validated @RequestBody UpdateRoleDTO param) {
        this.rolesService.update(param);
        return CommonResponse.success();
    }


    @GetMapping("/verifyRoleName")
    public CommonResponse verifyRoleName(@RequestParam String roleName) {
        this.rolesService.verifyRoleName(roleName);
        return CommonResponse.success();
    }

    @GetMapping("/list")
    public CommonResponse list(@RequestParam(value = "pageNo", defaultValue = "1") Long pageNo,
                               @RequestParam(value = "pageSize", defaultValue = "20") Long pageSize,
                               @RequestParam(value = "search", required = false) String search) {
        PageVO<RoleVO> result = this.rolesService.pageList(pageNo, pageSize,search);
        return CommonResponse.data(result);
    }

    @GetMapping("/info")
    public CommonResponse info(@RequestParam Long id) {
        RoleVO result = this.rolesService.info(id);
        return CommonResponse.data(result);
    }

    @GetMapping("/deleteById")
    public CommonResponse deleteById(@RequestParam Long id) {
        this.rolesService.deleteById(id);
        return CommonResponse.success();
    }

}
